package com.zr.daoimpl;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.zr.dao.DistributionRangePriceDaoLPN;
import com.zr.model.Distributionprice;
import com.zr.model.Distributionrangeprice;
import com.zr.vo.Drp_dpVO;

@Repository("drpDao")
public class DistributionRangePriceDaoImplLPN extends BaseDaOImpl implements DistributionRangePriceDaoLPN{

	@Override
	public List getDistributionRangePrice(int page,int rows,int uid) {
		
		String sql = " select drp.drpId,drp.drpFirstKilogramPrice,drp.drpNextKilogramPrice,drp.drpFirstCubicPrice,drp.drpNextCubicPrice,drp.drpRemarks,ot.otName,ds.dsName,dp.dpName  "
				+ "from distributionrangeprice as drp,ordertype as ot,distributionscope as ds,deliverypoint as dp,employeeinformation as ei "
				+ "where drp.otId=ot.otId and drp.dsId=ds.dsId and dp.dpId=ds.dpId and ei.dpId=dp.dpId and  ei.uid = :uid";
		Query query = this.getCurrentSession().createSQLQuery(sql);
		query.setInteger("uid", uid);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<Drp_dpVO> ss = query.setResultTransformer(Transformers.aliasToBean(Drp_dpVO.class)).list();
		return ss;
	}

	@Override
	public int getDistributionRangePriceCount() {
		String hql = "select count(drp.drpId) from Distributionrangeprice  drp";
    	Query query = this.getCurrentSession().createQuery(hql);
    	int count = ((Number) query.uniqueResult()).intValue();
		return count;
	}

	@Override
	public boolean insertDRP(Double drpFirstKilogramPrice, Double drpNextKilogramPrice, Double drpFirstCubicPrice,
			Double drpNextCubicPrice, String drpRemarks, int dsId, int otId) {
		boolean flag = false;
		String sql = "insert into distributionrangeprice(drpFirstKilogramPrice,drpNextKilogramPrice,drpFirstCubicPrice,drpNextCubicPrice,drpRemarks,dsId,otId) "
				+ "values(:drpFirstKilogramPrice,:drpNextKilogramPrice,:drpFirstCubicPrice,:drpNextCubicPrice,:drpRemarks,:dsId,:otId)";
		Query query = this.getCurrentSession().createSQLQuery(sql);
		query.setDouble("drpFirstKilogramPrice",drpFirstKilogramPrice);
		query.setDouble("drpNextKilogramPrice", drpNextKilogramPrice);
		query.setDouble("drpFirstCubicPrice", drpFirstCubicPrice);
		query.setDouble("drpNextCubicPrice", drpNextCubicPrice);
		query.setString("drpRemarks", drpRemarks);
		query.setInteger("dsId", dsId);
		query.setInteger("otId", otId);
		int i = query.executeUpdate();
		if (i>0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public int updateDRP(int drpId, Double drpFirstKilogramPrice, Double drpNextKilogramPrice,
			Double drpFirstCubicPrice, Double drpNextCubicPrice, String drpRemarks) {
		Distributionrangeprice drp = (Distributionrangeprice) this.getCurrentSession().get(Distributionrangeprice.class, drpId);
		drp.setDrpFirstKilogramPrice(drpFirstKilogramPrice);
		drp.setDrpNextKilogramPrice(drpNextKilogramPrice);
		drp.setDrpFirstCubicPrice(drpFirstCubicPrice);
		drp.setDrpNextCubicPrice(drpNextCubicPrice);
		drp.setDrpRemarks(drpRemarks);
		int i = drp.getDrpId();
		return i;
	}

	@Override
	public int deleteDRP(int drpId) {
		Distributionrangeprice drp = (Distributionrangeprice) this.getCurrentSession().get(Distributionrangeprice.class, drpId);
		this.getCurrentSession().delete(drp);
		int i = drp.getDrpId();
		return i;
	}

	@Override
	public int finddrpIdBydsIdAndotId(int dsId, int otId) {
		
		int drpId=0;
		try {

		String hql = "from Distributionrangeprice  drp  where drp.distributionscope.dsId=:dsId and drp.ordertype.otId=:otId";
		
		Query query = this.getCurrentSession().createQuery(hql);
		query.setInteger("dsId", dsId);
		query.setInteger("otId", otId);
		
		List<Distributionrangeprice>  dis = query.list();
		drpId=dis.get(0).getDrpId();
		
		} catch (Exception e) {
			//System.out.println("运行异常");
		}
		return drpId;

	}

}
